package cn.zzuli.sqlite;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

public class ShowActivity extends AppCompatActivity {
    private TextView one;
    private TextView two;
    private List<Article> articles;
    private ListView listView;
    private ArticleAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_show);
        listView = findViewById(R.id.lv);
        articles = getArticles();
        adapter = new ArticleAdapter(ShowActivity.this, articles);
        listView.setAdapter(adapter);

    }
    private List<Article> getArticles() {
        List<Article> articleList = new ArrayList<>();
          Intent intent = getIntent();
          Bundle bundle=intent.getExtras();

        articleList = (List<Article>) bundle.getSerializable("list");
        return articleList;
    }

    private class ArticleAdapter extends BaseAdapter {
        private Context context;
        private List<Article> articles;

        //一个ListView不一定只有一个item，需要在Adapter里面定义一个泛型为Article的一个list,即private List<Article> articles
        public ArticleAdapter(Context context, List<Article> articles) {
            this.context = context;
            this.articles = articles;

        }

        @Override
        public int getCount() {
            return articles.size();
        }

        @Override
        public Object getItem(int position) {
            return articles.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            convertView = LayoutInflater.from(ShowActivity.this).inflate(R.layout.show_data, parent, false);
            one = convertView.findViewById(R.id.tv_one);
            two = convertView.findViewById(R.id.tv_two);
            Article article = articles.get(position);
            one.setText(article.getName());
            two.setText(article.getTel());
            return convertView;
        }
    }
}